import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:flutter_xj_video/entity/film_entity.dart';
import 'package:get/get.dart';
import 'package:video_viewer/video_viewer.dart';

class FilmPlayPage extends StatefulWidget {
  const FilmPlayPage({Key key}) : super(key: key);

  @override
  _FilmPlayPageState createState() => _FilmPlayPageState();
}

class _FilmPlayPageState extends State<FilmPlayPage> {
  final VideoViewerController controller = VideoViewerController();
  FilmRssListVideo filmRssListVideo;

  @override
  void initState() {
    filmRssListVideo = Get.arguments;
    log(filmRssListVideo.dl.dd);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Size screen = MediaQuery.of(context).size;
    return Scaffold(
      appBar: AppBar(
        title: Text(filmRssListVideo.name),
      ),
      body: Container(
        width: screen.width,
        height: screen.width * 9 / 16,
        color: Colors.black,
        child: FutureBuilder<Map<String, VideoSource>>(
          future: VideoSource.fromM3u8PlaylistUrl(
            filmRssListVideo.dl.dd,
            formatter: (quality) =>
                quality == "Auto" ? "Automatic" : "${quality.split("x").last}p",
          ),
          builder: (_, data) {
            return data.hasData
                ? VideoViewer(
                    autoPlay: true,
                    source: data.data,
                    onFullscreenFixLandscape: false,
                    style: VideoViewerStyle(thumbnail: null),
                  )
                : SizedBox();
          },
        ),
      ),
    );
  }
}
